home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 954 < prev    next >
Encoding:
Text File  |  1996-08-06  |  3.7 KB  |  94 lines

  1. Path: fido.asd.sgi.com!austern
  2. From: kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Const class member
  5. Date: 03 Apr 1996 09:22:42 PST
  6. Organization: GABI Software, Sarl.
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <KANZE.96Apr3112803@slsvgqt.lts.sel.alcatel.de>
  9. References: <4jgpqa$t09@nntp.interaccess.com> <4jq60l$6pp@symiserver2.symantec.com>
  10. NNTP-Posting-Host: isolde.mti.sgi.com
  11. X-Original-Date: 03 Apr 1996 09:28:02 GMT
  12. In-Reply-To: thoff@symantec.com's message of 02 Apr 96 13:30:31 GMT
  13. Apparently-To: std-c++@ncar.ucar.edu
  14. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  15.     iQBVAwUBMWKz40y4NqrwXLNJAQHmqQH/QO9zQR156yDlXv183f8UEkbm7dIwNxja
  16.     unq6rIc8xa8ZI49b2kpg724yrO8MQWbOYW8r7vJjWMf6jjX+gtILzA==
  17.     =FWhZ
  18. Originator: austern@isolde.mti.sgi.com
  19.  
  20. In article <4jq60l$6pp@symiserver2.symantec.com> thoff@symantec.com
  21. (Torsten Hoff) writes:
  22.  
  23. |> In article <4jgpqa$t09@nntp.interaccess.com>,
  24. |>    brianmcg@interaccess.com (Brian V. McGroarty) wrote:
  25. |> >Is this legal?  Borland and Microsoft compilers will accept the following:
  26. |> >
  27. |> >class AnyClass
  28. |> >{
  29. |> >    const int constInt;
  30. |> >}
  31. |> >
  32. |> >The Borland compiler complains about the uninitialized constant, whereas
  33. |> >the Microsoft compiler does not.  If a constructor is present, both will
  34. |> >complain that the constant isn't initialized in the constructor, however
  35. |> >neither will allow you to assign a value in the constructor by simply
  36. |> >specifying "constInt= some value".  I have also attempted to initialize in
  37. |> >a global variable "int AnyClass::constInt",  to determine whether static
  38. |> >somehow became implicit -- still no go.  If this is legal, how is the value
  39. |> >initialized?
  40. |> [Snip]
  41.  
  42. |> That's perfectly legal.
  43.  
  44. |> However, you can't initialize the const member in the class
  45. |> declaration, since you haven't instantiated an object of the class in
  46. |> question which could receive the value. Furthermore, it would prevent
  47. |> you from assigning different values to constInt in different instances
  48. |> of the class, which in most cases is not what you want, either.
  49.  
  50. |> If you *really* want compile-time initialized constants, and can live
  51. |> with something that has essentially the same properties as an integer,
  52. |> use class-scope enums. If you need several compile-time constants with
  53. |> the same value, you can use multiple untagged enums:
  54.  
  55. |> class AnyClass
  56. |> {
  57. |>     enum {FOO = 1, BAR = 1}; // multiple enums with same value; doesn't work!
  58.  
  59. Why not?  It is allowed by the standard, and works on all of the
  60. compilers I've used.
  61.  
  62. |>     enum {FOO = 1};          // OK
  63. |>     enum {BAR = 1};          // OK
  64. |> }
  65.  
  66. The (minor) problem with this solution is that the type of the constant
  67. may not be what you want.  This is probably only a problem when function
  68. overloading is present, or templates.
  69.  
  70. The draft standard allows an initialization of a *static* const in the
  71. class definition, e.g.:
  72.  
  73.     class X
  74.     {
  75.         static size_t const     mySize = 1024 ;
  76.     } ;
  77.  
  78. This is a relatively new feature, however, and may not be supported by
  79. your compiler.
  80.  
  81.     [Rest of correct answer deleted...]
  82. -- 
  83. James Kanze         Tel.: (+33) 88 14 49 00        email: kanze@gabi-soft.fr
  84. GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
  85. Conseils, Θtudes et rΘalisations en logiciel orientΘ objet --
  86.                 -- A la recherche d'une activitΘ dans une region francophone
  87. ---
  88. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
  89.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  90.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  91.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  92.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  93. ]
  94.